#include "vtor_i2c.h"

#ifdef __VTOR_I2C__
#include "main.h"

// 加入用户工程宏
#ifdef __DEMO_PROJECT__



extern int32_t VtorShell_Scan(uint8_t*);


// 加入需要依赖的变量
// 使用背心 接收点led当作i2c

VtorI2c VtorI2cMaster_demoArray[3] = {0};
VtorI2c* qmcVtorI2c = NULL;
VtorI2c* masterI2c = NULL;
VtorI2c* oledI2c = NULL;
VtorI2c* mpu6050VtorI2c = NULL;
VtorI2c* stc8I2c = NULL;
VtorI2c* shellI2c = NULL;

typedef struct
{
	VtorI2c* i2c;
	GPIO_TypeDef* sclPort;
	uint16_t sclPin;
	GPIO_TypeDef* sdaPort;
	uint16_t sdaPin;
}VtorI2cMaster_Stm32HalPort;

const static VtorI2cMaster_Stm32HalPort VtorI2cMasterGpioArray[] = 
{
	{&VtorI2cMaster_demoArray[0], GPIOB, GPIO_PIN_6, GPIOB, GPIO_PIN_7},
	//{&VtorI2cMaster_demoArray[1], GPIOB, GPIO_PIN_8, GPIOB, GPIO_PIN_9},
	
	{NULL, NULL, 0, NULL, 0},
};



void VtorI2c_WriteScl(VtorI2c* i2c, uint8_t level)
{
	HAL_GPIO_WritePin(
		VtorI2cMasterGpioArray[i2c->i2cPortIdx].sclPort,
		VtorI2cMasterGpioArray[i2c->i2cPortIdx].sclPin,
		(GPIO_PinState)level);
}

uint8_t VtorI2c_ReadScl(VtorI2c* i2c)
{
	return HAL_GPIO_ReadPin(
		VtorI2cMasterGpioArray[i2c->i2cPortIdx].sclPort,
		VtorI2cMasterGpioArray[i2c->i2cPortIdx].sclPin);
}

void VtorI2c_WriteSda(VtorI2c* i2c, uint8_t level)
{
	HAL_GPIO_WritePin(
		VtorI2cMasterGpioArray[i2c->i2cPortIdx].sdaPort,
		VtorI2cMasterGpioArray[i2c->i2cPortIdx].sdaPin,
		(GPIO_PinState)level);

}

uint8_t VtorI2c_ReadSda(VtorI2c* i2c)
{
	return HAL_GPIO_ReadPin(
		VtorI2cMasterGpioArray[i2c->i2cPortIdx].sdaPort,
		VtorI2cMasterGpioArray[i2c->i2cPortIdx].sdaPin);
}

void VtorI2c_SetSdaDir(VtorI2c* i2c, uint8_t dir)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	if(dir == GPIO_DIR_OUTPUT)
	{
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
	}
	else
	{
		GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	}
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	
	GPIO_InitStruct.Pin = VtorI2cMasterGpioArray[i2c->i2cPortIdx].sdaPin;
	HAL_GPIO_Init(VtorI2cMasterGpioArray[i2c->i2cPortIdx].sdaPort, &GPIO_InitStruct);
	
}


// 初始化IIC的IO口 给函数指针赋值
void VtorI2c_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	
	// 根据不同实例进行细分配置
	for(int8_t i = 0; VtorI2cMasterGpioArray[i].i2c != NULL; i++)
	{
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
		GPIO_InitStruct.Pin = VtorI2cMasterGpioArray[i].sclPin;
		HAL_GPIO_Init(VtorI2cMasterGpioArray[i].sclPort, &GPIO_InitStruct);
		
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
		GPIO_InitStruct.Pin = VtorI2cMasterGpioArray[i].sdaPin;
		HAL_GPIO_Init(VtorI2cMasterGpioArray[i].sdaPort, &GPIO_InitStruct);
		
		VtorI2c_Stop(VtorI2cMasterGpioArray[i].i2c);
	}
	// multi 模式记得给 i2cPortIdx 赋值
	VtorI2cMaster_demoArray[0].i2cPortIdx = 0;

	qmcVtorI2c = &VtorI2cMaster_demoArray[0];
	mpu6050VtorI2c = &VtorI2cMaster_demoArray[0];
	oledI2c = &VtorI2cMaster_demoArray[0];
	shellI2c = &VtorI2cMaster_demoArray[0];
	stc8I2c = &VtorI2cMaster_demoArray[0];

}


void VtorI2cMaster_ReadBufferTask()
{
	uint8_t rxBuf[101] = {"-------"};;
	uint8_t rxBufLen = 0;
	rxBufLen = VtorI2cMaster_ReadBuffer(
		shellI2c, BUFFER_I2C_SLAVE_ADDR, rxBuf, 100);
	if(rxBufLen > 0)
	{
		VtorShell_Scan(rxBuf);
	}
}


inline void VtorI2c_Delay(uint32_t tick)
{

	//HAL_Delay(1);

	for(int i = 0; i < 400; i++)
		__NOP();
	__NOP();
	__NOP();
	__NOP();
}


#endif

#endif // __VTOR_I2C__

